package org.tio.sitexxx.service.service.qj;

import com.alibaba.fastjson.JSON;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import org.tio.sitexxx.service.model.main.Consultant;
import org.tio.sitexxx.service.model.main.Demo;
import org.tio.sitexxx.service.model.main.Knowledge;
import org.tio.sitexxx.service.vo.Const;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ConsultantService {
    public static final ConsultantService me = new ConsultantService();

    final Consultant dao = new Consultant().dao();

    public List<Consultant> getAll() {
        SqlPara sqlPara =   Db.use(Const.Db.TIO_SITE_MAIN).getSqlPara("consultant.getAll");
        List<Consultant> consultant =dao.find(sqlPara);
        return consultant;
    }

    /**
     * 更新顾问信息
     * @param name
     * @param mobile
     * @param openid
     * @return
     */
    public boolean update(String name,String mobile,String openid){
        String sqlid = "consultant.updatenameandmobile";
        String db = Const.Db.TIO_SITE_MAIN;
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid,name,mobile,openid);
        Db.use(db).update(sqlPara.getSql(),name,mobile,openid);
        return true;
    }
    /**
     * 删除数据
     * @param consultant
     * @return
     */
    public boolean deleteById(Integer consultant) {
        String sqlid = "consultant.setNull";
        String db = Const.Db.TIO_SITE_MAIN;
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid,consultant);
        Db.use(db).update(sqlPara.getSql(),consultant);
        return true;
    }
    /**
     * 同意注册顾问
     * @return
     */
    public List<Consultant> consultantAudit(){
        String sqlid = "consultant.consultantAudit";
        String db = Const.Db.TIO_SITE_MAIN;
        List<Consultant>resultList=new ArrayList<>();
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid);
        System.out.println(sqlPara.getSql());
        List<Record> record = Db.use(db).find(sqlPara);
        record.forEach((record1 -> {
            resultList.add(JSON.parseObject(JSON.toJSONString(record1.getColumns()),Consultant.class));
        }));
        if(!resultList.isEmpty()){
            return resultList;
        }
        return null;
    }

    public boolean update_staff_name(String name,String openid){
        String sqlid = "consultant.update_staff_name";
        String db = Const.Db.TIO_SITE_MAIN;
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid,name,openid);
        Db.use(db).update(sqlPara.getSql(),name,openid);
        return true;
    }

    /**
     * 回答问题的总数
     * @param query
     * @return
     */
    public Integer getDesignCount(Map<String, Object> query,int consultantId){
        String sqlid = "consultant.getDesignCount";
        String db = Const.Db.TIO_SITE_MAIN;
        Kv cond=Kv.create().set(query);
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid,cond);
        Integer  count = Db.use(db).queryInt(sqlPara.getSql(),sqlPara.getPara());
        String sql="select count(*) from consultant_member_valid where consultant_id=?";
        int kf_count=Db.use(db).queryInt(sql,consultantId);
        return count+kf_count;
    }

    /**
     * 根据id获取顾问
     * @param kv
     * @return
     */
    public Consultant getConsultant(Map<String, Object> kv) {
        SqlPara sqlPara =   Db.use(Const.Db.TIO_SITE_MAIN).getSqlPara("consultant.getConsultant",kv);
        Consultant consultant =dao.findFirst(sqlPara);
        return consultant;
    }

    /**
     * 根据openid获取顾问
     * @param openid
     * @return
     */
    public Consultant getConsultantByOpenId(String openid) {
        Consultant consultant =dao.findFirst("select * from consultant where openid =?",openid);
        return consultant;
    }

    /**
     * update
     * @param consultant
     * @return
     */
    public boolean update(Consultant consultant) {
        return consultant.update();
    }

    //pc
    //插入数据
    public boolean createConsultant(Consultant c) {
        return c.update();
    }

    //查重
    public boolean checkConsultant(String mobile) {

        SqlPara sqlPara = Db.use(Const.Db.TIO_SITE_MAIN).getSqlPara("consultant.checkConsultant",mobile);
        List<Consultant> list = dao.find(sqlPara);
        return (list.size() == 0);
    }


    public Consultant findConsultant(Kv kv) {
        SqlPara sqlPara = Db.use(Const.Db.TIO_SITE_MAIN).getSqlPara("consultant.findConsultant",kv);
        return dao.findFirst(sqlPara);
    }


    public Consultant singeConsultant(Kv kv) {
        SqlPara sqlPara = Db.use(Const.Db.TIO_SITE_MAIN).getSqlPara("consultant.singeConsultant",kv);
        return dao.findFirst(sqlPara);
    }


    public void  setNull(Consultant consultant){
        SqlPara sqlPara = Db.use(Const.Db.TIO_SITE_MAIN).getSqlPara("consultant.setNull",consultant);
        Db.update(sqlPara.getSql());
    }
}
